package com.pro.system.Controller;

import com.pro.system.Repository.CommentRepository;
import com.pro.system.entity.Comment;
import com.pro.system.entity.Movie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("admin/comments")
public class CommentController {

    @Autowired
    CommentRepository commentRepository;

    @GetMapping
    public String showComment (Model model){
        List<Comment> comment = commentRepository.findAll();
        model.addAttribute("comment",comment);
        return "web/background/comments/comments";
    }


    @GetMapping("intonew")
    public String intonew (Model model){
        Comment comment = new Comment();
        model.addAttribute(comment);
        return "web/background/comments/comments-new";
    }

    @PostMapping("save")
    public String save(Model model,Comment comment) {
        Comment save = commentRepository.save(comment);
        if (save != null) {
            return "web/background/comments/comments-new";
        }
        else {
            return "";
        }
    }
    @PostMapping("deleteItemByCheck")
    public String deleteItemByCheck(Model model, Integer[] getvalues){
        for(int i=0; i<getvalues.length;++i){
            commentRepository.deleteById(getvalues[i]);
        }
        return "redirect:/admin/comments";
    }

    @PostMapping("research")
    public String research(Model model,@RequestParam String byNameLike){
        List<Comment> byNameLikes = commentRepository.findByNameLike(byNameLike);
        model.addAttribute("comment",byNameLikes);
        return  "web/background/comments/comments";
    }
}
